﻿//This file is part of ILoveHUSTBoard.

//ILoveHUSTBoard is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.

//ILoveHUSTBoard is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with ILoveHUSTBoard.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataHandler.message
{
    public class PageProcessor
    {
        public string GetPageString(int currentPage,int pageItemCount)
        {
            MessageBoardDataContext _context=new MessageBoardDataContext();

            var resultList = from message in _context.TMessage
                             select message;

            long count = resultList.Count();

            if (count > 0)
            {
                string result = "";

                result += "<a href=\"index.aspx?page=1\">回首页</a>";

                result += string.Format("<a href=\"{0}\"><<</a>", currentPage == 1 ? "#" : "index.aspx?page=" + (currentPage - 1).ToString());

                if(currentPage!=1)
                    result += "<span> ... </span>";

                long totalPage = count % pageItemCount == 0 ? count / pageItemCount : (count / pageItemCount) + 1;

                for (int i = currentPage,j=0; j<=5 && i <= totalPage; i++,j++)
                {
                    if (i == currentPage)
                        result += string.Format("<a href=\"#\">{0}</a>", i.ToString());
                    else
                        result += string.Format("<a href=\"{0}\">{1}</a>", "index.aspx?page=" + i.ToString(), i.ToString());
                }

                if((currentPage+5)<totalPage)
                    result += "<span> ... </span>";

                result += string.Format("<a href=\"{0}\">>></a>",currentPage<totalPage?"index.aspx?page="+(currentPage+1).ToString():"#");

                return result;
            }
            else
            {
                return "";
            }
        }
    }
}
